package com.xqb.modules.controller.exam;

import com.xqb.common.annotation.Log;
import com.xqb.common.core.controller.BaseController;
import com.xqb.common.core.domain.AjaxResult;
import com.xqb.common.core.page.TableDataInfo;
import com.xqb.common.enums.BusinessType;
import com.xqb.common.utils.poi.ExcelUtil;
import com.xqb.modules.domain.dto.ExaQuestionDto;
import com.xqb.modules.domain.po.ExaQuestion;
import com.xqb.modules.domain.vo.ExamQuestionDetailVo;
import com.xqb.modules.service.IExaQuestionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 题目Controller
 *
 * @author xqb
 * @date 2024-10-14
 */
@RestController
@RequestMapping("/exam/question")
public class ExaQuestionController extends BaseController
{
    @Autowired
    private IExaQuestionService exaQuestionService;

    /**
     * 查询题目列表
     */
    @PreAuthorize("@ss.hasPermi('exam:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExaQuestion exaQuestion)
    {
        startPage();
        List<ExamQuestionDetailVo> list = exaQuestionService.getExaQuestionList(exaQuestion);
        return getDataTable(list);
    }

    /**
     * 导入模板
     *
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ExaQuestionDto> util = new ExcelUtil<>(ExaQuestionDto.class);
        util.importTemplateExcel(response, "题目数据");
    }

    /**
     * 导入数据
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<ExaQuestionDto> util = new ExcelUtil<>(ExaQuestionDto.class);
        List<ExaQuestionDto> questionDtos = util.importExcel(file.getInputStream());
        String message = exaQuestionService.importByExcel(questionDtos);
        return success(message);
    }
    /**
     * 导出题目列表
     */
    @PreAuthorize("@ss.hasPermi('exam:question:export')")
    @Log(title = "题目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExaQuestion exaQuestion)
    {
        List<ExaQuestion> list = exaQuestionService.selectExaQuestionList(exaQuestion);
        ExcelUtil<ExaQuestion> util = new ExcelUtil<ExaQuestion>(ExaQuestion.class);
        util.exportExcel(response, list, "题目数据");
    }


    /**
     * 获取题目详细信息
     */
    @PreAuthorize("@ss.hasPermi('exam:question:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(exaQuestionService.selectExaQuestionById(id));
    }

    /**
     * 新增题目
     */
    @PreAuthorize("@ss.hasPermi('exam:question:add')")
    @Log(title = "题目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExaQuestionDto exaQuestion) {
        return toAjax(exaQuestionService.insertExaQuestion(exaQuestion));
    }

    /**
     * 修改题目
     */
    @PreAuthorize("@ss.hasPermi('exam:question:edit')")
    @Log(title = "题目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExaQuestionDto exaQuestion) {
        return toAjax(exaQuestionService.updateExaQuestion(exaQuestion));
    }

    /**
     * 删除题目
     */
    @PreAuthorize("@ss.hasPermi('exam:question:remove')")
    @Log(title = "题目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(exaQuestionService.deleteExaQuestionByIds(ids));
    }
    /**
     * 批量查询与业务有关的题目id
     */
    @ApiOperation("批量查询与业务有关的题目id")
    @PreAuthorize("@ss.hasPermi('exam:question:list')")
    @GetMapping("/questionList/{id}")
    public AjaxResult questionList(@PathVariable("id") Long id)
    {
        return success(exaQuestionService.questionList(id));
    }
}
